Ext.define('ArvoreKD.ux.data.proxy.Server', {
    extend: 'Ext.data.proxy.Server',
    alias : 'proxy.uxserver',
    
    processResponse: function(operation, request, response, callback, scope){
		operation.cache = true;
		
		var me = this,
            reader,
            result,
            records,
            length,
            mc,
            record,
            i;
            
        reader = me.getReader();
        result = reader.read(me.extractResponseData(response));
        records = result.records;
        length = records.length;
		
        if (result.success !== false) {
            mc = Ext.create('Ext.util.MixedCollection', true, function(r) {return r.getId();});
            mc.addAll(operation.records);
            for (i = 0; i < length; i++) {
                record = mc.get(records[i].getId());
                
                if (record) {
                    record.beginEdit();
                    record.set(record.data);
                    record.endEdit(true);
                }
            }
            
            //see comment in buildRequest for why we include the response object here
            Ext.apply(operation, {
                response: response,
                resultSet: result
            });
            
            operation.setCompleted();
            operation.setSuccessful();
        } else {
            operation.setException(result.message);
            me.fireEvent('exception', this, response, operation);
        }
            
        //this callback is the one that was passed to the 'read' or 'write' function above
        if (typeof callback == 'function') {
            callback.call(scope || me, operation);
        }
            
        me.afterRequest(request);
    },
    
    buildUrl: function(request) {
        var me = this,
            url = me.getUrl(request);
        
        //<debug>
        if (!url) {
            Ext.Error.raise("You are using a ServerProxy but have not supplied it with a url.");
        }
        //</debug>
        
        return url;
    }
});
